package com.csi.sai.servlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import sun.dc.DuctusRenderingEngine;

import com.csi.sai.bl.viewfilters.ViewFiltersProcess;
import com.csi.sai.data.entities.GenericFilters;
import com.csi.sai.utilities.Constants;
import com.csi.sai.utilities.GenericEntitie;
import com.csi.sai.utilities.Utilities;
import com.csi.sai.utilities.templates.selects.ComboClient;
import com.csi.sai.utilities.templates.selects.ComboEstatus;
import com.csi.sai.utilities.templates.selects.ComboVendedor;
import com.csi.sai.utilities.templates.selects.ComponentTemplate;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class ViewFiltersServlet
 */
@WebServlet("/web/ViewFiltersServlet")
public class ViewFiltersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private InitialContext envContext;
	private DataSource ds;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewFiltersServlet() {
        super();
        try {
			envContext = new InitialContext();
			Context initContext = (Context) envContext.lookup("java:/comp/env");
			ds = (DataSource) initContext.lookup("SAIDB");
		} catch (NamingException e) {
			e.printStackTrace();
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject jsonReq = JSONObject.fromObject(request.getParameter("jsonReq"));
		ViewFiltersProcess viewFilters = new ViewFiltersProcess(ds);
		JSONObject jsonResponse = new JSONObject();
		boolean succesfully = false;
		
		String process = jsonReq.getString("process");
		if(process.equals(Constants.PROCESS_FILTERDEMAND)) {
			int type = jsonReq.getInt("type");
			succesfully = viewFilters.getDinamicFiltersBy(type, getGFilters(jsonReq), Utilities.getClassTemplateBy(type));
			if(succesfully) {
				jsonResponse.put("Flag", "true");
				jsonResponse.put("HTMLCode", viewFilters.getHTMLCode());
			}
		}
		
		sendResponse(response, jsonResponse);
	}

	private GenericFilters getGFilters(JSONObject jsonReq) {
		GenericFilters gFilters = new GenericFilters();
		gFilters.setIdEmpleado(jsonReq.getInt("idEmpleado"));
		gFilters.setIdCliente(jsonReq.getInt("idCliente"));
		return gFilters;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	public void sendResponse(HttpServletResponse response, JSONObject jsonResponse) throws IOException {
		response.setContentType("text/xml; charset=iso-8859-1");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(jsonResponse);
	}

}
